package com.library.controller;

import com.alibaba.fastjson.JSONObject;
import com.library.entity.Book;
import com.library.entity.User;
import com.library.entity.UserVoteBook;
import com.library.entity.UserVoteBookId;
import com.library.service.IBookService;
import com.library.service.IUserVoteBookService;
import com.library.util.Json;
import com.library.util.Options;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;

/**
 * 书籍处理
 * Created by linzhi on 2016/12/6.
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Resource
    private IBookService bookService;
    @Resource
    private IUserVoteBookService userVoteBookService;

    /**
     * 荐书
     * @param book 书的信息
     */
    @RequestMapping("/save")
    public String save(Book book, HttpServletRequest request) {
        //获取荐书的用户（当前登录的用户）
        User user = (User) request.getSession().getAttribute("loginUser");

        book.setUser(user);
        book.setCreateTime(new Timestamp(new Date().getTime()));
        book.setStatus(Book.BOOK_STATUS_RECOMMENDED);
        bookService.save(book);
        return "redirect:/home/historyUI.do";
    }

    /**
     * 获取荐书列表
     * @param options 选项
     * @return 包含数据与结果信息
     */
    @RequestMapping("/getBookList")
    @ResponseBody
    public Json getBookList(Options options,HttpServletRequest request) {
        //参数校验
        if (options.getPageNo() == null) {
            options.setPageNo(1);
        }
        if (options.getPageSize() == null) {
            options.setPageSize(Options.OPTIONS_PAGESIZE_DEFAULT);
        }

        Json json = new Json();
        //为了判断荐书列表书目中当前用户有没有投票过
        User user = (User) request.getSession().getAttribute("loginUser");
        if (user != null) {
            options.setLoginUserId(user.getId());
        }

        JSONObject myPagination = bookService.findBooksByPagination(options);
        json.setData(myPagination);
        json.setStatus(true);
        return json;
    }

    /**
     * 投票
     * @param book 投票的书
     */
    @RequestMapping("/voteBook")
    @ResponseBody
    public Json voteBook(Book book,HttpServletRequest request) {
        Json json = new Json();
        User user = (User) request.getSession().getAttribute("loginUser");

        //保存投票实体
        UserVoteBook userVoteBook = new UserVoteBook();
        UserVoteBookId id = new UserVoteBookId();
        id.setUserId(user.getId());
        id.setBookId(book.getId());
        userVoteBook.setCreateTime(new Timestamp(new Date().getTime()));
        userVoteBook.setId(id);
        userVoteBookService.save(userVoteBook);

        json.setStatus(true);
        return json;
    }

    /**
     * 取消投票
     * @param book 投票的书
     */
    @RequestMapping("/disVoteBook")
    @ResponseBody
    public Json disVoteBook(Book book,HttpServletRequest request) {
        Json json = new Json();
        User user = (User) request.getSession().getAttribute("loginUser");

        //删除投票实体
        UserVoteBook userVoteBook = new UserVoteBook();
        UserVoteBookId id = new UserVoteBookId();
        id.setUserId(user.getId());
        id.setBookId(book.getId());
        userVoteBook.setId(id);
        userVoteBookService.delete(userVoteBook);

        json.setStatus(true);
        return json;
    }

    /**
     * 书的status+1
     * @param book
     * @return
     */
    @RequestMapping("/addBookStatus")
    @ResponseBody
    public Json addBookStatus(Book book) {
        Json json = new Json();
        bookService.addBookStatus(book);
        json.setStatus(true);
        return json;
    }

    /**
     * 书的status-1
     * @param book
     * @return
     */
    @RequestMapping("/reduceBookStatus")
    @ResponseBody
    public Json reduceBookStatus(Book book) {
        Json json = new Json();
        bookService.reduceBookStatus(book);
        json.setStatus(true);
        return json;
    }
}
